/*
 HALinput.cpp library for input in the washingmashine
 */



#include <Arduino.h> 

#include "HALinput.h"


#define OUT_KEYSELECT   3
#define IN_W2           16
#define IN_W1           17
#define IN_T2           18
#define IN_T1           19
#define IN_IN3          20
#define IN_IN2          21
#define IN_IN1          22
#define IN_IN0          23

HALinput::HALinput(Centipede CS)
{
  door = false;
  pressure = false;
  soap1 = false;
  soap2 = false;
  door_old = false;
  pressure_old = false;
  soap1_old = false;
  soap2_old = false;
  ten_old = false;
  fifty_old = false;
  twohundred_old = false;
  program_old = false;
  start_old = false;
  clear_old = false;
  change = false;
  temperature_old = 0;
  water_old = 0;
  this->CS = CS;
  //update();
}

void HALinput::update()
{
  CS.digitalWrite(OUT_KEYSELECT,LOW);
  if(CS.digitalRead(IN_IN3) == HIGH){
    door = true;
  }
  else{
    door = false;
  }
  if(CS.digitalRead(IN_IN2) == HIGH){
    soap2 = true;
  }
  else{
    soap2 = false;
  }
  if(CS.digitalRead(IN_IN1) == HIGH){
    soap1 = true;
  }
  else{
    soap1 = false;
  }
  if(CS.digitalRead(IN_IN0) == HIGH){
    pressure = true;
  }
  else{
    pressure = false;
  }

  CS.digitalWrite(OUT_KEYSELECT,HIGH);
  if(CS.digitalRead(IN_IN3) == HIGH && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == LOW){
    if(CS.digitalRead(IN_IN3) == HIGH && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == LOW){
      if(ten_old == false){
        ten_tmp = true;
        ten_old = true;
      }
    }
  }
  else{
    ten_old = false;
  }//10
  if(CS.digitalRead(IN_IN3) == LOW && CS.digitalRead(IN_IN2) == HIGH && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == LOW){
    if(CS.digitalRead(IN_IN3) == LOW && CS.digitalRead(IN_IN2) == HIGH && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == LOW){
      if(fifty_old == false){
        fifty_tmp = true;
        fifty_old = true;
      }
    }
  }
  else{
    fifty_old = false;
  }//50
  if(CS.digitalRead(IN_IN3) == LOW && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == HIGH && CS.digitalRead(IN_IN0) == LOW){
    if(CS.digitalRead(IN_IN3) == LOW && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == HIGH && CS.digitalRead(IN_IN0) == LOW){
      if(twohundred_old == false){
        twohundred_tmp = true;
        twohundred_old = true;
      }
    }
  }
  else{
    twohundred_old = false;
  }//200
  if(CS.digitalRead(IN_IN3) == LOW && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == HIGH){
    if(CS.digitalRead(IN_IN3) == LOW && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == HIGH){
      if(start_old == false){
        start_tmp = true;
        start_old = true;
      }
    }
  }
  else{
    start_old = false;
  }//start
  if(CS.digitalRead(IN_IN3) == HIGH && CS.digitalRead(IN_IN2) == HIGH && CS.digitalRead(IN_IN1) == HIGH && CS.digitalRead(IN_IN0) == LOW){
    if(CS.digitalRead(IN_IN3) == HIGH && CS.digitalRead(IN_IN2) == HIGH && CS.digitalRead(IN_IN1) == HIGH && CS.digitalRead(IN_IN0) == LOW){
      if(clear_old == false){
        clear_tmp = true;
        clear_old = true;
      }
    }
  }
  else{
    clear_old = false;
  }//clear
  if(CS.digitalRead(IN_IN3) == HIGH && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == HIGH){
    if(CS.digitalRead(IN_IN3) == HIGH && CS.digitalRead(IN_IN2) == LOW && CS.digitalRead(IN_IN1) == LOW && CS.digitalRead(IN_IN0) == HIGH){
      if(program_old == false){
        program_tmp = true;
        program_old = true;
      }
    }
  }
  else{
    program_old = false;
  }//program
  if(CS.digitalRead(IN_W1) == HIGH){
    if(CS.digitalRead(IN_W2) == HIGH){
      water = 3;
    }
    else{
      water = 1;
    }
  }
  else if(CS.digitalRead(IN_W2) == HIGH){
    water = 2;
  }
  else{
    water = 0;
  }
  if(CS.digitalRead(IN_T1) == HIGH){
    if(CS.digitalRead(IN_T2) == HIGH){
      temperature = 3;
    }
    else{
      temperature = 1;
    }
  }
  else if(CS.digitalRead(IN_T2) == HIGH){
    temperature = 2;
  }
  else{
    temperature = 0;
  }
  checkChange();
}

void HALinput::checkChange(){
  if(door != door_old){
    change = true;
  }
  if(pressure != pressure_old){
    change = true;
  }
  if(soap1 != soap1_old){
    change = true;
  }
  if(soap2 != soap2_old){
    change = true;
  }
  if(ten_tmp){
    change = true;
  }
  if(fifty_tmp){
    change = true;
  }
  if(twohundred_tmp){
    change = true;
  }
  if(start_tmp){
    change = true;
  }
  if(program_tmp){
    change = true;
  }
  if(clear_tmp){
    change = true;
  }
  if(water != water_old){
    change = true;
    water_old = water;
  }
  if(temperature != temperature_old){
    change = true;
    temperature_old = temperature;
  }
}

bool HALinput::readTen(){
  if(ten_tmp){
    ten_tmp = false;
    return true;
  }
  return false;
}
bool HALinput::readFifty(){
  if(fifty_tmp){
    fifty_tmp = false;
    return true;
  }
  return false;
}
bool HALinput::readTwoHundred(){
  if(twohundred_tmp){
    twohundred_tmp = false;
    return true;
  }
  return false;
}
bool HALinput::readStart(){
  if(start_tmp){
    start_tmp = false;
    return true;
  }
  return false;
}
bool HALinput::readProgram(){
  if(program_tmp){
    program_tmp = false;
    return true;
  }
  return false;
}
bool HALinput::readClear(){
  if(clear_tmp){
    clear_tmp = false;
    return true;
  }
  return false;
}
bool HALinput::readDoor(){
  door_old = door;
  return door;
}
bool HALinput::readPressure(){
  pressure_old = pressure;
  return pressure;
}
bool HALinput::readSoap1(){
  soap1_old = soap1;
  return soap1;
}
bool HALinput::readSoap2(){
  soap2_old = soap2;
  return soap2;
}
int HALinput::readWater(void){
  water_old = water;
  return water;
}
int HALinput::readTemperature(void){
  temperature_old = temperature;
  return temperature;
}
void HALinput::clearChange(){
  change = false;
  ten_old = false;
  fifty_old = false;
  twohundred_old = false;
  program_old = false;
  start_old = false;
  clear_old = false;
  ten_tmp = false;
  fifty_tmp = false;
  twohundred_tmp = false;
  program_tmp = false;
  start_tmp = false;
  clear_tmp = false;
}

